﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SoulHackerCUnite.Data;
using SoulHackerCUnite.DataAdapter;
using SoulHackerCUnite.Extensions;
using SoulHackerCUnite.Functions;

namespace WinForm3._5
{
    public partial class MainForm : Form
    {
        private List<Demon> _demons;
        private List<UniteRule> _rules;
        private List<Race> _races;
        private Unite _unite = new Unite();

        private BindingCollection<ResultToShow> _resultsToShow;
        
        public MainForm()
        {
            InitializeComponent();
            LoadDatas();
        }

        private void LoadDatas()
        {
            SqliteAdapter adapter = new SqliteAdapter();
            adapter.DataFile = ".\\soulhackersc.db3";
            adapter.Open();

            _demons = adapter.FillDemons();
            _rules = adapter.FillRules();
            _races = adapter.FillRaces();

            _unite.Skills = adapter.FillSkills();
            _unite.SkillInherts = adapter.FillSkillInhert();

            adapter.Close();

            _unite.Demons = _demons;
            _unite.Rules = _rules;
            _unite.Races = _races;

            _resultsToShow = new BindingCollection<ResultToShow>();

            dgDemons.AutoGenerateColumns = false;
            dgDemons.DataSource = new BindingCollection<Demon>(_demons);

            dgDemons.RowHeadersWidth = 10;
            dgResult.RowHeadersWidth = 10;
            dgResult.AutoGenerateColumns = false;
            dgResults.RowHeadersWidth = 10;
            dgResults.AutoGenerateColumns = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (dgDemons.SelectedRows.Count == 2) // twin unite
            {
                Demon demon1 = dgDemons.SelectedRows[0].DataBoundItem as Demon;
                Demon demon2 = dgDemons.SelectedRows[1].DataBoundItem as Demon;

                Demon result = _unite.Unite2(demon1, demon2);
                if (result == null)
                    MessageBox.Show("Can not unite this two");
                else
                {
                    ResultToShow resultToShow = new ResultToShow() { Demon1 = demon1, Demon2 = demon2, Result = result };
                    _resultsToShow.Clear();
                    _resultsToShow.Add(resultToShow);
                    //dgResults.Items.Clear();
                    //dgResults.Items.Add(resultToShow);
                    ////MessageBox.Show(result.Level + " " + result.RaceJ + " " + result.Name);
                    //dgResult.Rows.Clear();
                    //dgResult.Rows.Add(result);
                    dgResult.DataSource = new BindingCollection<Demon>() { result };
                }
            }

            else if (dgDemons.SelectedRows.Count == 3) // triple unite
            {
                Demon demon1 = dgDemons.SelectedRows[0].DataBoundItem as Demon;
                Demon demon2 = dgDemons.SelectedRows[1].DataBoundItem as Demon;
                Demon demon3 = dgDemons.SelectedRows[2].DataBoundItem as Demon;

                Demon result = _unite.Unite3(demon1, demon2, demon3);
                if (result == null)
                    MessageBox.Show("Can not unite this three");
                else
                {
                    ResultToShow resultToShow = new ResultToShow() { Demon1 = demon1, Demon2 = demon2, Demon3 = demon3, Result = result, Plus2 = "+" };
                    _resultsToShow.Clear();
                    _resultsToShow.Add(resultToShow);
                    //dgResults.Items.Clear();
                    //dgResults.Items.Add(resultToShow);
                    ////MessageBox.Show(result.ToString());
                    //dgResult.Items.Clear();
                    //dgResult.Items.Add(result);
                    dgResult.DataSource = new BindingCollection<Demon>() { result };
                }
            }
            else if (dgDemons.SelectedRows.Count > 3) // triple unite
            {
                List<Demon> demons = new List<Demon>();
                foreach (DataGridViewRow selectedItem in dgDemons.SelectedRows)
                {
                    demons.Add(selectedItem.DataBoundItem as Demon);
                }
                _resultsToShow = new BindingCollection<ResultToShow>(_unite.UniteMuti(demons));
                if (_resultsToShow != null)
                {
                    demons = new List<Demon>();
                    foreach (ResultToShow toShow in _resultsToShow)
                    {
                        demons.CheckAdd(toShow.Result);
                    }
                    /*dgResult.Items.Clear();
                    foreach (Demon demon in demons)
                    {
                        dgResult.Items.Add(demon);
                    }*/
                    dgResult.DataSource = new BindingCollection<Demon>(demons);
                }
            }
        }

        private void dgResult_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                Demon result = dgResult.Rows[e.RowIndex].DataBoundItem as Demon;
                if (result != null)
                {
                    ////dgResults.Items.Clear();
                    dgResults.DataSource =
                        new BindingCollection<ResultToShow>(
                            _resultsToShow.ToList().FindAll(r => r.Result.Name == result.Name));
                }
            }
        }

        private void bUnunite_Click(object sender, EventArgs e)
        {
            if (dgDemons.SelectedRows.Count > 0)
            {
                _resultsToShow.Clear();
                //dgResult.Clear();
                List<Demon> demons = new List<Demon>();
                List<ResultToShow> results = new List<ResultToShow>();

                foreach (DataGridViewRow selectedItem in dgDemons.SelectedRows)
                {
                    Demon demon = selectedItem.DataBoundItem as Demon;
                    if (demon != null)
                    {
                        List<ResultToShow> ununite2 = _unite.UnUnite2(demon);
                        if (ununite2 != null && ununite2.Count > 0)
                        {
                            results.AddRange(ununite2);
                            //dgResult.Items.Add(demon);
                        }

                        List<ResultToShow> ununite3 = _unite.UnUnite3(demon);
                        if (ununite3 != null && ununite3.Count > 0)
                        {
                            results.AddRange(ununite3);
                            //dgResult.Items.Add(demon);
                        }

                        if ((ununite2 != null && ununite2.Count > 0) || (ununite3 != null && ununite3.Count > 0))
                        {
                            demons.Add(demon);
                        }
                    }
                }
                _resultsToShow = new BindingCollection<ResultToShow>(results);
                dgResult.DataSource = new BindingCollection<Demon>(demons);

            }
        }
    }
}
